const express = require('express');
const router = express.Router()
const { db, genid } = require('../db/DbConnect');

router.get('/test', async (req, res) => {
    db.all('select * from admin', [], (err, rows) => {
        if (err) {
            console.error(err.message);
        } else {
            console.log('The rows are:', rows);
        }
    })

    // let _dbres = await db.async.all('select * from admin')
    // console.log(_dbres);

    res.send({
        id: genid.NextId(),
        // _dbres
    })
})
// router.get('/list', async (req, res) => {
//     let { keyword, category_id, page, pageSize } = req.query
//     page = page == null ? 1 : page;
//     pageSize = pageSize == null ? 10 : pageSize
//     category_id = category_id == null ? 0 : category_id
//     keyword = keyword == null ? "" : keyword

//     let params = []
//     let whereSqls = []

//     if (category_id != 0) {
//         whereSqls.push(` category_id =? `)
//         params.push(category_id)
//     }

//     if (keyword != "") {
//         whereSqls.push(` (title LIKE ? OR content LIKE ?) `)
//         params.push("%" + keyword + "%")
//         params.push("%" + keyword + "%")
//     }

//     let whereSqlStr = ""
//     if (whereSqls.length > 0) {
//         whereSqlStr = " WHERE " + whereSqls.join("AND")
//     }

//     //查分页数据
//     let searchSql = ` SELECT * FROM blog `
//         + whereSqlStr + ` ORDER BY create_time DESC LIMIT ?, ? `
//     let searchSqlParams = params.concat([(page - 1) * pageSize, pageSize])

//     //查询数据总数
//     let searchCountsql = ` SELECT count(*) AS count FROM blog ` + whereSqlStr;
//     let searchCountParans = params

//     // 分页数据
//     // let searchResult = await db.async.all(searchSql, searchSqlParams)
//     // 总数数据
//     // let countResult = await db.async.all(searchCountsql, searchCountParans)

//     try {
//         let [searchResult, countResult] = await Promise.all([
//             await db.async.all(searchSql, searchSqlParams),
//             await db.async.all(searchCountsql, searchCountParans)
//         ])

//         if (searchResult.length > 0 && countResult[0].count) {
//             res.status(200).send({
//                 code: 200,
//                 msg: '获取成功',
//                 count: countResult[0].count,
//                 data: searchResult
//             })
//         } else {
//             throw new Error('搜索条件有误')
//         }
//     } catch (error) {
//         res.status(500).send({
//             code: 500,
//             msg: '获取失败',
//             error: error.message
//         })
//     }
// })

module.exports = router
